﻿#include "FITKAcousticsFESubSysMgrAdaptor.h"
#include "FITKAcousticsHDF5Writer.h"
#include "FITKAcousticsHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
#include"FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"
#include"FITK_Component/FITKAcousticsModel/FITKFESubSysAcoustic.h"
#include"FITK_Component/FITKAcousticsModel/FITKFESubSysStructure.h"
#include"FITK_Component/FITKAcousticsModel/FITKFESubSysGeoProp.h"

bool IO::FITKAcousticsFESubSysMgrAdaptor::adaptR()
{
    Acoustics::FITKAcousticsFESubSysManager* FESubSysManager =
        dynamic_cast<Acoustics::FITKAcousticsFESubSysManager*>(_dataObj);
    if (FESubSysManager == nullptr || _reader == nullptr) return false;
    m_toolp = _reader->getHDF5FileTool();//初始化工具

    std::tuple<bool, H5::Group>Ret = m_toolp->openGp("FESubSys");//打开FE子系统
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group gp = std::get<1>(Ret);


    return this->readFESubSysMgr(gp, FESubSysManager);
}

bool IO::FITKAcousticsFESubSysMgrAdaptor::adaptW()
{
    Acoustics::FITKAcousticsFESubSysManager* FESubSysManager =
        dynamic_cast<Acoustics::FITKAcousticsFESubSysManager*>(_dataObj);
    if (FESubSysManager == nullptr || _writer == nullptr) return false;
    m_toolp = _writer->getHDF5FileTool();//初始化工具



    std::tuple<bool, H5::Group> Ret = m_toolp->createGp("FESubSys");     //创建FE子系统
    if (std::get<0>(Ret) == false) { return false; }
    H5::Group gp = std::get<1>(Ret);

    return this->writeFESubSysMgr(gp, FESubSysManager);

}

bool IO::FITKAcousticsFESubSysMgrAdaptor::readFESubSysMgr(H5::Group & gp, Acoustics::FITKAcousticsFESubSysManager * FESubSysManager)
{

    int count = -1;
    m_toolp->readGroupint(gp, "count", &count);

    for (int i = 0; i < count; ++i)
    {
        QString number = QString::number(i);
        std::tuple<bool, H5::Group>Ret = m_toolp->openGp(gp, number.toStdString().data());//打开子节点
        if (std::get<0>(Ret) == false) { return false; }
        H5::Group subGroup = std::get<1>(Ret);
        int typeint = -1;
        m_toolp->readGroupint(subGroup, "type", &typeint);
        if (typeint == 1)
        {
            return false;
        }
        else if (typeint == 2)
        {
            Acoustics::FITKFESubSysStructure *subobject = new Acoustics::FITKFESubSysStructure;
            this->readParent(subGroup, subobject);
            int DampingLossSpectraID =-1;//阻尼损失谱ID
            m_toolp->readGroupint(subGroup, "DampingLossSpectraID", &DampingLossSpectraID);
            subobject->setDampingLossSpectraID(DampingLossSpectraID);
            FESubSysManager->appendDataObj(subobject);
        }
        else if(typeint == 3)
        {
            Acoustics::FITKFESubSysAcoustic *subobject = new Acoustics::FITKFESubSysAcoustic;
            this->readParent(subGroup, subobject);
            int DampingSpectraID = -1;//阻尼谱ID
            m_toolp->readGroupint(subGroup, "DampingSpectraID", &DampingSpectraID);
            subobject->setDampingSpectraID(DampingSpectraID);
            FESubSysManager->appendDataObj(subobject);
        }
    }
    return true;
}

bool IO::FITKAcousticsFESubSysMgrAdaptor::writeFESubSysMgr(H5::Group & gp, Acoustics::FITKAcousticsFESubSysManager * FESubSysManager)
{
    //遍历全部对象
    const int count = FESubSysManager->getDataCount();

    m_toolp->writeGroupAttrInt(gp, "count", count);
    for (int i = 0; i < count; ++i)
    {
        std::tuple<bool, H5::Group>subRet = m_toolp->createGp(gp, QString::number(i));//创建子节点的子节点
        if (std::get<0>(subRet) == false) { return false; }
        H5::Group subGroup = std::get<1>(subRet);

        Acoustics::FITKAbstractFESubSys* sys = FESubSysManager->getDataByIndex(i);
        if (sys == nullptr) continue;
        this->writeParent(subGroup, sys);
        Acoustics::FITKAbstractFESubSys::FESubSysType type = sys->getFESubSysType();//类型
        int typeint = -1;
        switch (type)
        {
        case Acoustics::FITKAbstractFESubSys::FENone:
        {
            typeint = 1;
            break;
        }
        case Acoustics::FITKAbstractFESubSys::FEStructure:
        {
            typeint = 2;
            Acoustics::FITKFESubSysStructure *subobject = dynamic_cast<Acoustics::FITKFESubSysStructure*>(sys);
            if (subobject == nullptr)
            {
                return false;
            }
            int DampingLossSpectraID = subobject->getDampingLossSpectraID();//阻尼损失谱ID
            m_toolp->writeGroupAttrInt(subGroup, "DampingLossSpectraID", DampingLossSpectraID);
            break;
        }

        case Acoustics::FITKAbstractFESubSys::FEAcoustic:
        {
            typeint = 3;
            Acoustics::FITKFESubSysAcoustic *subobject = dynamic_cast<Acoustics::FITKFESubSysAcoustic*>(sys);
            if (subobject == nullptr)
            {
                return false;
            }
            int DampingSpectraID = subobject->getDampingSpectraID();//阻尼谱ID
            m_toolp->writeGroupAttrInt(subGroup, "DampingSpectraID", DampingSpectraID);
            break;
        }
        default:
            break;
        }
        m_toolp->writeGroupAttrInt(subGroup, "type", typeint);
    }
    return true;
}

bool IO::FITKAcousticsFESubSysMgrAdaptor::writeParent(H5::Group & gp, Acoustics::FITKAbstractFESubSys * sys)
{
    if (sys == nullptr)
    {
        return false;
    }
    QString DataObjectName = sys->getDataObjectName();
    int DataObjectID = sys->getDataObjectID();
    m_toolp->CheckString(DataObjectName);
    m_toolp->writeGroupAttrStr(gp, "DataObjectName", DataObjectName.toStdString());
    m_toolp->writeGroupAttrInt(gp, "DataObjectID", DataObjectID);

    QList<int> ElementsID = sys->getElementsID();//单元ID列表
    //Acoustics::FITKFESubSysGeoProp* GeoProp=sys->getGeoProp();//几何属性--不写
    std::vector<int>temp = ElementsID.toVector().toStdVector();
    m_toolp->CreateOneDarrayDataSetInt(gp, "ElementsID", temp);

    bool enabledState = sys->getEnableState();//启用状态

    m_toolp->writeGroupAttrBool(gp, "enabledState", enabledState);

    QString Note = sys->getNote();//注释
    m_toolp->CheckString(Note);
    m_toolp->writeGroupAttrStr(gp, "Note", Note.toStdString());
    return true;
}

bool IO::FITKAcousticsFESubSysMgrAdaptor::readParent(H5::Group & gp, Acoustics::FITKAbstractFESubSys * sys)
{
    if (sys==nullptr)
    {
        return false;
    }
    std::string DataObjectName="";
    int DataObjectID =-1;
    

    m_toolp->readGroupStr(gp, "DataObjectName", DataObjectName);
    m_toolp->readGroupint(gp, "DataObjectID", &DataObjectID);
    QString name = QString::fromStdString(DataObjectName);
    m_toolp->RestoreString(name);

    //写回ID和名称
    sys->setDataObjectName(name);
    Core::FITKAbstractDataIDAlter a;
    a.modifyDataID(sys, DataObjectID);

    std::vector<int>temp;
    QList<int> ElementsID;
    m_toolp->ReadOneDArrayDataSetInt(gp, "ElementsID", temp);
    for (int id : temp)
    {
        ElementsID.push_back(id);
    }
    sys->setElements(ElementsID);//设置单元


    bool enabledState =false;//启用状态
    m_toolp->readGroupBool(gp, "enabledState", &enabledState);
    sys->setEnableState(enabledState);

    std::string Note ="";//注释
    m_toolp->readGroupStr(gp, "Note", Note);
    QString NoteQ = QString::fromStdString(Note);
    m_toolp->RestoreString(NoteQ);
    sys->setNote(NoteQ);

    sys->updateGeoProp();
    return true;
}

